<?php
namespace app\admin\controller;
use think\Db;
use think\Request;
use think\Controller;
class Article extends Common{
    //会员列表
    public function index(){
        if(request()->isPost()){
            $key=input('post.key');
            $page =input('page')?input('page'):1;
            $pageSize =input('limit')?input('limit'):config('pageSize');
            $list=db('article')
                ->where('title','like',"%".$key."%")
                ->order('id desc')
                ->paginate(array('list_rows'=>$pageSize,'page'=>$page))
                ->toArray();
            return $result = ['code'=>0,'msg'=>'获取成功!','data'=>$list['data'],'count'=>$list['total'],'rel'=>1];
        }
        return $this->fetch();
    }
	public function add(){
        if(request()->isPost()){
            $user = db('users');
            $data = input('post.');
           
            if(empty($data['password'])){
                unset($data['password']);
            }else{
                $data['password'] = md5($data['password']);
            }
            if ($user->update($data)!==false) {
                $result['msg'] = '会员修改成功!';
                $result['url'] = url('index');
                $result['code'] = 1;
            } else {
                $result['msg'] = '会员修改失败!';
                $result['code'] = 0;
            }
            return $result;
        }else{
            $category = db('category')->where("moduleid", 2)->select();
			$this->assign('category', $category);
            return $this->fetch();
        }
    }
	
    public function edit($id=''){
        if(request()->isPost()){
            $user = db('users');
            $data = input('post.');
           
            if(empty($data['password'])){
                unset($data['password']);
            }else{
                $data['password'] = md5($data['password']);
            }
            if ($user->update($data)!==false) {
                $result['msg'] = '会员修改成功!';
                $result['url'] = url('index');
                $result['code'] = 1;
            } else {
                $result['msg'] = '会员修改失败!';
                $result['code'] = 0;
            }
            return $result;
        }else{
            $user_level=db('upgrade')->select();
            $info = db('users')->where("id", $id)->find();
            $this->assign('info',json_encode($info,true));
            $this->assign('user',$info);
            $this->assign('title',lang('edit').lang('user'));
            $this->assign('user_level', $user_level);

            return $this->fetch();
        }
    }
	
	
    public function delall(){
        $map['id'] =array('in',input('param.ids/a'));
        db('users')->where($map)->delete();

        $result['msg'] = '删除成功！';
        $result['code'] = 1;
        $result['url'] = url('index');
        return $result;
    }
	
}